//
//  MyPercentDrivenInteractiveTransition.m
//  手势转场动画
//
//  Created by iOS on 16/7/18.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "MyPercentDrivenInteractiveTransition.h"
@interface MyPercentDrivenInteractiveTransition(){
 BOOL  _shouldCompleteTransition;
}
@property(nonatomic,strong)UIViewController * FromVC;
@property(nonatomic,strong)UIViewController * ToVC;
@end
@implementation MyPercentDrivenInteractiveTransition
static  float fraction = 0;
- (void)writeFromoViewController:(UIViewController*)fromVC AndToViewController:(UIViewController*)toVC{
    self.FromVC = fromVC;
    self.ToVC = toVC;
    [self addGestureRecognizerInFromView:toVC.view];
}

- (void)addGestureRecognizerInFromView:(UIView*)fromView{
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(geatureRecognizer:)];
    [fromView addGestureRecognizer:pan];
}
-(CGFloat)completionSpeed
{
    return 1-self.percentComplete ;
}

- (void)geatureRecognizer:(UIPanGestureRecognizer*)GestureRecognizer{
    CGPoint translation = [GestureRecognizer translationInView:GestureRecognizer.view.superview];
    NSLog(@"==== %f",translation.x);
    switch (GestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            self.interactionInProgress = YES;

            [self.ToVC.navigationController popViewControllerAnimated:YES];
            break;
        case UIGestureRecognizerStateChanged:{
            fraction =  -(translation.x / 200);
            fraction = fmin(fmaxf(fraction, 0.0), 1.0);
            NSLog(@"fraction === %f",translation.x);
            _shouldCompleteTransition = fraction > 0.5;
            //    _shouldCompleteTransition = 1;
            [self updateInteractiveTransition:fraction];
        }
            break;
     case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        {
            /**
             *  需要判断是结束了还是取消了
             */
            
            self.interactionInProgress = NO;
            
            if (!_shouldCompleteTransition || GestureRecognizer.state == UIGestureRecognizerStateCancelled)
            {
                //只要有一个不成立 那么就是取消了
                
                [self cancelInteractiveTransition];
            }
            
            else
            {
                [self finishInteractiveTransition];
            }
        }

        default:
            break;
    }
}
@end
